package cn.ljy.authority.web.controller.backstage.authority;


import cn.ljy.authority.service.SysUserService;
import cn.ljy.authority.service.SysWechatUserService;
import cn.ljy.authority.utils.JwtUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "微信小程序授权")
@RestController
@RequestMapping("/backstage")
public class AuthController {

    @Autowired
    private SysWechatUserService wechatUserService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private JwtUtils jwtUtils;

    /*@PostMapping("/wechat/login")
    public ResponseDTO<LoginResult> wechatLogin(@Validated @RequestBody WechatLoginRequest request) {
        try {
            SysUser user = wechatUserService.loginOrRegisterByWechat(request.getCode());

            if (user == null) {
                throw new BusinessException("微信登录失败");
            }

            String token = jwtUtils.generateToken(user.getId(), user.getUsername());
            LoginResult result = new LoginResult(token, user.getId(), user.getUsername(), user.getNickName());
            result.setAvatar(user.getHeadImgUrl());
            result.setNewUser(false); // 可根据实际业务判断

            return ResponseDTO.success(result);
        } catch (Exception e) {
            throw new BusinessException("登录失败: " + e.getMessage());
        }
    }*/

//    @PostMapping("/wechat/bind")
//    public ResponseDTO<LoginResult> bindWechat(@Validated @RequestBody WechatBindRequest request) {
//        try {
//            // 验证用户名密码
//            SysUser user = userService.getUserByUsername(request.getUsername());
//            if (user == null || !userService.validatePassword(request.getPassword(), user.getPassword())) {
//                throw new BusinessException("用户名或密码错误");
//            }
//
//            // 获取微信session
//            var session = wechatUserService.getWechatSession(request.getCode());
//
//            // 绑定微信
//            wechatUserService.bindWechatUser(user.getId(), session.getOpenid(), session.getSession_key());
//
//            // 生成token
//            String token = jwtUtils.generateToken(user.getId(), user.getUsername());
//            LoginResult result = new LoginResult(token, user.getId(), user.getUsername(), user.getNickName());
//
//            return ResponseDTO.success(result);
//        } catch (Exception e) {
//            throw new BusinessException("绑定失败: " + e.getMessage());
//        }
//    }

//    @PostMapping("/logout")
//    public ResponseDTO<?> logout(@RequestHeader("Authorization") String token) {
//        // JWT是无状态的，客户端需要自行删除token
//        return ResponseDTO.success("退出成功");
//    }
}